package masterjava.common.settings;

import masterjava.common.convertion.FileConverter;
import masterjava.common.logging.ServerLogger;
import masterjava.common.shared.EntryMap;
import masterjava.common.shared.logging.LoggingLevel;
import masterjava.common.shared.settings.AppSettingKeys;
import masterjava.common.store.IPropertyListener;
import masterjava.common.store.Property;

import java.io.File;

import static masterjava.common.shared.settings.AppSettingKeys.*;

/**
 * User: GKislin
 * Date: 22.01.11
 * <p/>
 * Singleton
 */

public class AppProps implements SettingValues {

    public static Property<String> APP_ID;

    public static Property<String> APP_NAME;

    public static Property<Integer> VSC_REVISION;

    public static Property<LoggingLevel> LOGGING_LEVEL;

    public static Property<String> COMPANY_NAME;

    public static Property<File> APPLICATION_HELP_FILE;
    public static Property<String> APPLICATION_HELP;

    public void setPropsFactory(PropsFactory propsFactory) {
        APP_ID = propsFactory.create(appId);
        APP_NAME = propsFactory.create(appName);
        VSC_REVISION = propsFactory.create(vscRevision, 0);
        COMPANY_NAME = propsFactory.create(companyName);
        LOGGING_LEVEL = propsFactory.create(loggingLevel, LoggingLevel.INFO);
        LOGGING_LEVEL.addListener(new IPropertyListener<LoggingLevel>() {
            @Override
            public void update(Property<LoggingLevel> prop, LoggingLevel newValue) {
                ServerLogger.setRootLoggingLevel(newValue);
            }
        });
        APPLICATION_HELP_FILE = propsFactory.create(AppSettingKeys.applicationHelpFile, FileConverter.get());
        APPLICATION_HELP = propsFactory.createFieStore(AppSettingKeys.applicationHelp, APPLICATION_HELP_FILE);
    }

    public static EntryMap createSettingValues() {
        EntryMap map = new EntryMap();
        map.addProp(APP_ID, APP_NAME, VSC_REVISION, COMPANY_NAME, APPLICATION_HELP_FILE, APPLICATION_HELP);
        return map;
    }

    @Override
    public EntryMap getSettingValues() {
        return createSettingValues();
    }
}
